<template>
	<div class="api-scan-config">
		<a-form-item label="扫描模块" name="scanModules">
			<a-select
				v-model:value="selectedModules"
				mode="multiple"
				placeholder="请选择要扫描的模块"
				@change="handleScanModulesChange"
			>
				<a-select-option value="dashboard">仪表盘模块</a-select-option>
				<a-select-option value="sys">系统管理模块</a-select-option>
				<a-select-option value="auth">认证模块</a-select-option>
				<a-select-option value="dev">开发工具模块</a-select-option>
				<a-select-option value="gen">代码生成模块</a-select-option>
				<a-select-option value="biz">业务模块</a-select-option>
			</a-select>
			<div class="form-help-text">选择要扫描API接口的模块，留空则扫描所有模块</div>
		</a-form-item>

		<a-form-item label="扫描选项">
			<a-space direction="vertical" style="width: 100%">
				<a-checkbox v-model:checked="onlyGetMethods" @change="handleOptionsChange"> 仅扫描GET方法</a-checkbox>
				<a-checkbox v-model:checked="enablePermissionCheck" @change="handleOptionsChange"> 启用权限验证</a-checkbox>
				<a-checkbox v-model:checked="excludeSystemApis" @change="handleOptionsChange"> 排除系统级API</a-checkbox>
			</a-space>
		</a-form-item>

		<a-form-item label="排除路径" name="excludePaths">
			<a-select
				v-model:value="excludePaths"
				mode="tags"
				placeholder="输入要排除的API路径"
				@change="handleExcludePathsChange"
			>
				<a-select-option value="/error">/error</a-select-option>
				<a-select-option value="/actuator">/actuator</a-select-option>
				<a-select-option value="/swagger-ui">/swagger-ui</a-select-option>
				<a-select-option value="/api-docs">/api-docs</a-select-option>
			</a-select>
			<div class="form-help-text">指定要排除的API路径模式</div>
		</a-form-item>
	</div>
</template>

<script setup>
	import { ref, watch } from 'vue'

	// Props
	const props = defineProps({
		modelValue: {
			type: Object,
			default: () => ({})
		}
	})

	// Emits
	const emit = defineEmits(['change', 'change'])

	// 扫描配置
	const selectedModules = ref([])
	const onlyGetMethods = ref(true)
	const enablePermissionCheck = ref(true)
	const excludeSystemApis = ref(true)
	const excludePaths = ref([])

	// 监听props变化
	watch(
		() => props.modelValue,
		(newVal) => {
			if (newVal) {
				try {
					selectedModules.value = JSON.parse(newVal.scanModules || '["dashboard", "sys"]')
				} catch (error) {
					selectedModules.value = ['dashboard', 'sys']
				}

				onlyGetMethods.value = newVal.onlyGetMethods === 'true'
				enablePermissionCheck.value = newVal.enablePermissionCheck === 'true'
				excludeSystemApis.value = newVal.excludeSystemApis === 'true'

				try {
					excludePaths.value = JSON.parse(newVal.excludePaths || '[]')
				} catch (error) {
					excludePaths.value = []
				}
			}
		},
		{ deep: true, immediate: true }
	)

	// 更新配置
	const updateConfig = () => {
		const config = {
			scanModules: JSON.stringify(selectedModules.value),
			onlyGetMethods: onlyGetMethods.value.toString(),
			enablePermissionCheck: enablePermissionCheck.value.toString(),
			excludeSystemApis: excludeSystemApis.value.toString(),
			excludePaths: JSON.stringify(excludePaths.value)
		}
		emit('change', config)
	}

	// 处理扫描模块变化
	const handleScanModulesChange = () => {
		updateConfig()
	}

	// 处理选项变化
	const handleOptionsChange = () => {
		updateConfig()
	}

	// 处理排除路径变化
	const handleExcludePathsChange = () => {
		updateConfig()
	}

	// 获取扫描配置
	const getScanConfig = () => {
		return {
			scanModules: JSON.stringify(selectedModules.value),
			onlyGetMethods: onlyGetMethods.value.toString(),
			enablePermissionCheck: enablePermissionCheck.value.toString(),
			excludeSystemApis: excludeSystemApis.value.toString(),
			excludePaths: JSON.stringify(excludePaths.value)
		}
	}

	// 验证配置
	const validateConfig = () => {
		if (selectedModules.value.length === 0) {
			return { valid: false, message: '请至少选择一个扫描模块' }
		}

		return { valid: true }
	}

	// 暴露方法给父组件
	defineExpose({
		getScanConfig,
		validateConfig
	})
</script>

<style scoped>
	.api-scan-config {
		padding: 0;
	}

	.form-help-text {
		font-size: 12px;
		color: #999;
		margin-top: 4px;
	}
</style>
